#include "StdAfx.h"
#include "HttpClient.h"


CHttpClient::CHttpClient()   
{

}

CHttpClient::~CHttpClient()   
{

}

CString   CHttpClient::doGet(CString   href)   
{
	CString httpsource;
	CInternetSession session1;
	session1.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT,5000);
	CHttpFile* pHTTPFile=NULL;
	try{
		pHTTPFile=(CHttpFile*)session1.OpenURL(href);
		//session1.
	}catch(CInternetException){
		pHTTPFile=NULL;
	}
	if(pHTTPFile)
	{
		CString text;
		for(;pHTTPFile->ReadString(text);)
		{
			httpsource=httpsource+text;
		}
		pHTTPFile->Close();
		delete   pHTTPFile;
	}
	else
	{

	}
	return httpsource;
}

CString CHttpClient::doPost(CString href)
{
	CString httpsource;
	CInternetSession session1;
	CHttpConnection* conn1=NULL;
	CHttpFile* pFile = NULL;
	CString strServerName;
	CString strObject; 
	INTERNET_PORT nPort;
	DWORD dwServiceType;
	AfxParseURL((LPCTSTR)href,dwServiceType, strServerName, strObject, nPort);
	DWORD retcode; 
	char* outBuff = CONTENT.GetBuffer(1000);
	try
	{
		conn1 = session1.GetHttpConnection(strServerName,nPort);
		pFile = conn1->OpenRequest(0,strObject,NULL,1,NULL,"HTTP/1.1",INTERNET_FLAG_EXISTING_CONNECT|INTERNET_FLAG_NO_AUTO_REDIRECT);
		pFile -> AddRequestHeaders("Content-Type: application/x-www-form-urlencoded");
		pFile -> AddRequestHeaders("Accept:   */*");
		//pFile -> SendRequest(NULL,0,outBuff,strlen(outBuff)+1);
		//::AfxMessageBox(CONTENT);
		pFile -> SendRequest(NULL,0,CONTENT.GetBuffer(),CONTENT.GetLength());
		pFile -> QueryInfoStatusCode(retcode);
	}
	catch (CInternetException * e){};
	if(pFile)
	{
		CString text;
		for(;pFile->ReadString(text);)
		{
			httpsource=httpsource+text;
		}
		pFile->Close();
	}else
	{

	}
	return httpsource;
	delete pFile;
	delete conn1;
	session1.Close();
}

void CHttpClient::addParam(CString name,CString value)
{   
	names.AddTail((LPCTSTR)name);
	values.AddTail((LPCTSTR)value);
//	CString eq="=";
//	CString an="&";
	CONTENT=CONTENT+name+"="+value+"&";
	CL=CONTENT.GetLength();
}  
